/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/6/27
 * Time: 16:59
 */

#include "OverlapLayout.h"
#include <QWidget>
namespace Upgrade {
    OverlapLayout::OverlapLayout(QWidget *parent /* = nullptr*/, int spacing /* = 50*/, const QString &_order_property /*= "OrderProperty"*/)
        : QLayout(parent)
        , overlapSpacing(spacing)
        , order_property_name_(_order_property)
    {
        setSpacing(overlapSpacing);
    }

    OverlapLayout::~OverlapLayout()
    {
        QLayoutItem *item;
        while((item = takeAt(0)) != nullptr)
            delete item;
    }

    void OverlapLayout::Resort()
    {
        Q_ASSERT(nullptr != sortFunction);
        std::sort(itemList.begin(), itemList.end(), sortFunction);
        this->update();
    }

    void OverlapLayout::addItem(QLayoutItem *item)
    {
        itemList.append(item);
        Resort();
    }

    QSize OverlapLayout::sizeHint() const
    {
        if(itemList.isEmpty())
            return {};

        QSize size;
        size.setWidth(overlapSpacing * (itemList.size() - 1) + itemList.last()->sizeHint().width());
        size.setHeight(itemList.last()->sizeHint().height());

        auto margin = contentsMargins();
        return size += QSize({margin.left() + margin.right(),margin.top() + margin.bottom()});
    }

    QSize OverlapLayout::minimumSize() const
    {
        if(itemList.isEmpty())
            return {};

        QSize size;
        size.setWidth(overlapSpacing * (itemList.size() - 1) + itemList.last()->minimumSize().width());
        size.setHeight(itemList.last()->minimumSize().height());

        auto margin = contentsMargins();
        return size += QSize({margin.left() + margin.right(),margin.top() + margin.bottom()});
    }

    int OverlapLayout::count() const
    {
        return itemList.size();
    }

    QLayoutItem *OverlapLayout::itemAt(int index) const
    {
        return itemList.value(index);
    }

    QLayoutItem *OverlapLayout::takeAt(int index)
    {
        if(index >= 0 && index < itemList.size())
            return itemList.takeAt(index);
        return nullptr;
    }

    void OverlapLayout::setGeometry(const QRect &rect)
    {
        QLayout::setGeometry(rect);
        doLayout(rect);
    }

    void OverlapLayout::doLayout(const QRect &rect)
    {
        int left, top, right, bottom;
        getContentsMargins(&left, &top, &right, &bottom);
        QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);

        // 计算中心点
        auto center = effectiveRect.center();

        int x = center.x();
        int y = center.y();

        // 根据牌数计算最左侧的偏移值
        if(itemList.empty())
            return;

        auto width = (itemList.size() - 1) * overlapSpacing + itemList.last()->sizeHint().width();

        auto start = x - width / 2.0;
        for(auto item : itemList) {
            QRect geom(start, y - item->sizeHint().height() / 2, item->sizeHint().width(), item->sizeHint().height());

            item->setGeometry(geom);
            item->widget()->raise();
            start += overlapSpacing;
        }
    }

}// namespace Upgrade